import { createSlice, createAsyncThunk } from '@reduxjs/toolkit'
import {
  getHomeDiscountData,
  getHomeGoodPriceData,
  getHomeHighScoreData,
  getHomeHotRecommendData, getHomeLongForData, getHomePlusData,
} from '@/services'

// 异步调用接口
export const fetchHomeDataAction = createAsyncThunk('fetchData', (payload, {dispatch, getState}) => {

  const state = getState();
  getHomeGoodPriceData().then(res => {
    dispatch(changeGoodPriceInfoAction(res))
  })
  // 高分数据
  getHomeHighScoreData().then(res => {
    dispatch(changeHighScoreInfoAction(res))
  })
//   折扣数据请求
  getHomeDiscountData().then(res => {
    dispatch(changeDiscountInfoAction(res));
  })
//   热门推荐
  getHomeHotRecommendData().then(res => {
    dispatch(changeHotRecommendInfoAction(res));
  })
  getHomeLongForData().then(res=>{
    dispatch(changeLongForInfoAction(res));
  })
  getHomePlusData().then(res=>{
    dispatch(changeHomePlusInfoAction(res))
  })
})

const homeSlice = createSlice({
  name: "home",
  initialState: {
    goodPriceInfo: {},
    highScoreInfo: {},
    discountInfo: {},
    hotRecommendInfo: {},
    longForInfo:{},
    homePlusInfo:{}

  },
  reducers: {
    changeGoodPriceInfoAction(state, {payload}){
      state.goodPriceInfo = payload
    },
    changeHighScoreInfoAction(state, {payload}){
      state.highScoreInfo = payload;
    },
    changeDiscountInfoAction(state, {payload}){
      state.discountInfo = payload;
    },
    changeHotRecommendInfoAction(state, {payload}){
      state.hotRecommendInfo = payload;
    },
    changeLongForInfoAction(state,{payload}){
      state.longForInfo = payload;
    },
    changeHomePlusInfoAction(state,{payload}){
      state.homePlusInfo = payload;
    }
  }
})
// 导出action
export const {
  changeGoodPriceInfoAction,
  changeHighScoreInfoAction,
  changeDiscountInfoAction,
  changeHotRecommendInfoAction,
  changeLongForInfoAction,
  changeHomePlusInfoAction,
} = homeSlice.actions

export default homeSlice.reducer
